<?php
/*'****************************************************************************\
    CHTML 0.2.1
 	Copyright (C) 2007, 2008 Alexey Ilyin

 	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published
	by the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.

 	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

 	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

\******************************************************************************/

require_once 'chtml.inc.php';

class CCustomMessage extends CHTMLTemplate {
    protected $messages = array();
    
    function messages ($clear = TRUE) {
        $messages = $this->messages;
        if ($clear) $this->messages = array();
        return $messages;
    }
    
    function count () {
        return count($this->messages);
    }

    /**
     *  Service method.
     *  Use as:
     *  @code:
     *  static function from ($place[,$args]) {
     *      return self::$msg->get(func_get_args());
     *  }
     *  @endcode
     */
    protected function get ($args) {
        $place = array_shift($args);
        return call_user_func_array(array($this,
            strtr($place, ':', '_')), $args);
    }
    protected function add ($args) {
        $this->messages[] = $this->get($args);
        return count($this->messages);
    }
    /**
     *  arg1, arg2, ...  => class="c0"...
     *  array(key1 => arg1, key2 => arg2, ...)
     */
    function getMessage (/*$place[,$args]*/) {
        $args = func_get_args();
        $place = array_shift($args);
        if (is_array($args[0])) $args = $args[0];
        $msg = $this->element("#$place", 0);
        if (!$msg) {
            return $this->unknownMessage($place, $args);
        }
        foreach ($args as $selector => $value) {
            $msg->element(is_int($selector) ? ".c$selector" : $selector,
                'set', 'nodeValue', $value);
        }
        return $msg->source();
    }
    /**
     *  @see getMessage
     */
    function unknownMessage ($place, $args) {
        return <<<HTML
<span class="unknown-message">$place</span>
HTML;
    }
    /**
     *  @see getMessage
     */
    function __call ($place, $args) {
        return $this->getMessage($place, $args);
    }
}


class CMessage extends CCustomMessage {
    protected static $core;

    static function create ($class, $template) {
        self::$core or self::$core = new $class();
        self::$core->loadFrom($template);
        return self::$core;
    }

    static function from (/*$place[,$args]*/) {
        return self::$core->add(func_get_args());
    }

    static function all () {
        return self::$core->messages(TRUE);
    }

    static function displayIn ($container, $display = 'block') {
        if (self::$core->count()) {
            $container->set('innerHTML',
                join("\r\n", self::$core->messages()));
            $container['style'] = "display:$display";
        }
        else {
            $container['style'] = 'display:none';
        }
    }
}

?>
